package com.yc.cloudvideo.sp_store.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.cloudvideo.entity.Comment;
import com.yc.cloudvideo.entity.CommentUser;
import com.yc.cloudvideo.sp_store.mapper.CommentMapper;
import com.yc.cloudvideo.sp_store.mapper.CommentUserMapper;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLNonTransientException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LYYT
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/cloudvideo/commentUser")
public class CommentUserController {

    @Resource
    CommentUserMapper commentUserMapper;
    @Resource
    CommentMapper commentMapper;

    @PostMapping("dianzan")
    void dianzan(@RequestParam("pid")long pid,
           long uid
    ){
        CommentUser commentUser=new CommentUser();
        commentUser.setPid(pid);
        commentUser.setUid(uid);
        try {
            commentUserMapper.insert(commentUser);
        }catch (Exception e){
            LambdaQueryWrapper<CommentUser> lqw=new LambdaQueryWrapper<>();
            lqw.setEntity(commentUser);
            commentUser= commentUserMapper.selectOne(lqw);
            commentUser.setExist(!commentUser.getExist());
            LambdaQueryWrapper<Comment> lqw1=new LambdaQueryWrapper<>();
            Comment comment= commentMapper.selectById(pid);
            if(commentUser.getExist()){
                comment.setLikenum(comment.getLikenum()+1);
            }else{
                comment.setLikenum(comment.getLikenum()-1);
            }
            commentMapper.updateById(comment);
            lqw.eq(CommentUser::getPid,pid);
            lqw.eq(CommentUser::getUid,uid);
            commentUserMapper.update(commentUser,lqw);
        }

    }
}

